<?php
namespace Vioscope;

if ( ! class_exists( '\Vioscope\Channel' ) ) :

class Channel {
	// YT channel id
	public $id;
	private $data;
	// Processed data
	public $info;
	public $page; // TODO: implement.
	public $total_page; // TODO: implement.

	function __construct( $channel_id = null, $page = null ) {
		if ( $channel_id ) {
			$this->set_channel_id( $channel_id, $page );
		}
	}

	public function set_channel_id( $channel_id = null, $page = null ) {
		if ( $channel_id ) {
			$this->id = $channel_id;
		} // TODO: Add check for invalid channel id
		if ( ! $page ) {
			$this->page = 1;
		} else {
			$this->page = intval( $page );
		}
		// Clear previous data (if any)
		unset( $this->data );
		unset( $this->info );
		// Update data
		$this->fetch_data( $this->id, $this->page );
	}

	public function fetch_data( $channel_id = null, $page = null ) {
		if ( ! $channel_id ) {
			$channel_id = $this->id;
		}
		if ( ! $page ) {
			$page = $this->page;
		} else {
			$this->page = $page;
		}
		
		$rsstext = get_url_contents( "https://www.youtube.com/feeds/videos.xml?channel_id={$this->id}&page=4" );
		$this->data = simplexml_load_string( $rsstext );
		$this->info['id'] = $this->id;
		if ( $this->data === false ) {
			echo "Failed loading XML: ";
			foreach( libxml_get_errors() as $error ) {
				echo "<br>", $error->message;
			}
		} else {
			$this->info['id'] = $this->id;
			$this->info['title'] = $this->data->title[0];
			$this->info['yt_url'] = $this->data->author->uri[0];
			$this->info['url'] = get_local_url( $this->id, 'channel' );
			// Playlist has the channel id but instead has 'UU' at the start
			$channel_playlist_id = substr_replace($this->id, 'UU', 0, 2);
			// Items in channel
			$body = get_url_contents( "https://www.youtube.com/playlist?list={$channel_playlist_id}&playnext=1&index=1" );
			$initial_data = get_initial_data( $body );
			if ( $initial_data ) {
				$this->info['entries'] = extract_items( $initial_data );
			}
			//$this->info['entries'] = $this->get_channel_entries();
		}
	}
	
	private function get_channel_entries() {
		if ( ! isset( $this->data ) ) return;
		$entries = array();
		foreach ( $this->data->entry as $entry ) {
			$video_id = explode( ':', $entry->id )[2];
			$entries[] = array(
				'id' => $video_id,
				'title' => (string) $entry->title,
				'yt_url' => (string) $entry->link->attributes()->href,
				'url' => get_local_url( $video_id ),
				'thumbnail_url_medium' => get_video_thumbnail_url( $video_id, 'medium' ),
				'thumbnail_url_small' => get_video_thumbnail_url( $video_id, 'small' ),
				'published' => (string) $entry->published,
				'updated' => (string) $entry->updated,
			);
		}
		return $entries;
	}
	
	/**
	 * Gives the channel page url for next, previous or based on defined page
	 * number.
	 *
	 * @param bool|int $next For next page set it to true, false for previous
	 *    and any integer for specific page.
	 * @return bool|str Either page url or false.
	 */
	// TODO: Create get_next_page_url and get_previous_page_url functions 
	// utilizing this one.
	// TODO: Implement/encode attributes according to: https://github.com/iv-org/invidious/blob/452d1e8307d6344dd51c5437ccd032a566291c34/src/invidious/channels.cr#L399
	public function get_change_page_url( $next = true ) {
		$page = $this->page;
		if ( ! is_numeric( $next ) ) {
			if ( $next == true ) {
				$page++;
			} else {
				$page--;
			}
		} else {
			$page = $next;
		}
		if ( $page < 1 ) { // TODO: Add $this->total_page in logic
			return false;
		}
		return get_base_url() . '/channel/' . $this->id . '?page=' . $page;
	}

}

endif;
